//
//  QZHHUD.m
//  QZH
//
//  Created by 米翊米 on 2017/9/5.
//  Copyright © 2017年 🐨🐨🐨. All rights reserved.
//

#import "QZHHUD.h"
#import "MBProgressHUD.h"

//weak self
#define QZHWS(weakSelf)            __weak __typeof(&*self)weakSelf = self;

@interface QZHHUD ()

@property (nonatomic, strong) UIView *targetView;
@property (nonatomic, strong) MBProgressHUD *loadHUD;
@property (nonatomic, strong) MBProgressHUD *textHUD;

@end

@implementation QZHHUD

/**
 单例

 @return QZHHUD实例
 */
+ (QZHHUD *)HUD {
    static QZHHUD *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[QZHHUD alloc] init];
    });
    
    return instance;
}

#pragma makr - 加载提示HUD

/**
 加载中HUD，使用系统activity
 */
- (void)loadingHUD {
    QZHWS(weakSelf);
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf hiddenHUD];
        
//        weakSelf.loadHUD = [[MBProgressHUD alloc] initWithView:weakSelf.targetView];
        weakSelf.loadHUD.mode = MBProgressHUDModeIndeterminate;
        weakSelf.loadHUD.label.text = nil;
        weakSelf.loadHUD.customView = nil;
        weakSelf.loadHUD.removeFromSuperViewOnHide = YES;
            
        [weakSelf showHUD:weakSelf.loadHUD];
    });
}

/**
 加载中HUD

 @param imageNamed 自定义加载中图片
 */
- (void)loadingHUDWithImage:(NSString *)imageNamed {
    QZHWS(weakSelf);
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf hiddenHUD];
        
//        weakSelf.loadHUD = [[MBProgressHUD alloc] initWithView:weakSelf.targetView];
        weakSelf.loadHUD.mode = MBProgressHUDModeCustomView;
        weakSelf.loadHUD.customView = [weakSelf gifViewWithLocal:imageNamed];
        weakSelf.loadHUD.bezelView.color = [UIColor clearColor];
        weakSelf.loadHUD.label.text = nil;
        weakSelf.loadHUD.removeFromSuperViewOnHide = YES;
        
        [weakSelf showHUD:weakSelf.loadHUD];
    });
}

/**
 加载中HUD
 
 @param message 使用系统activity并显示的文字
 */
- (void)loadingHUDWithMessage:(NSString *)message {
    QZHWS(weakSelf);
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf hiddenHUD];
        
//        weakSelf.loadHUD = [[MBProgressHUD alloc] initWithView:weakSelf.targetView];
        weakSelf.loadHUD.mode = MBProgressHUDModeIndeterminate;
        weakSelf.loadHUD.label.text = message;
        weakSelf.loadHUD.customView = nil;
        weakSelf.loadHUD.removeFromSuperViewOnHide = YES;
        
        [weakSelf showHUD:weakSelf.loadHUD];
    });
}

/**
 加载中HUD

 @param imageNamed 自定义图片
 @param message 同时显示的文字
 */
- (void)loadingHUDWithImage:(NSString *)imageNamed message:(NSString *)message {
    QZHWS(weakSelf);
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf hiddenHUD];
        
//        weakSelf.loadHUD = [[MBProgressHUD alloc] initWithView:weakSelf.targetView];
        weakSelf.loadHUD.mode = MBProgressHUDModeCustomView;
        weakSelf.loadHUD.customView = [weakSelf gifViewWithLocal:imageNamed];
        weakSelf.loadHUD.label.text = message;
        weakSelf.loadHUD.removeFromSuperViewOnHide = YES;
        
        [weakSelf showHUD:weakSelf.loadHUD];
    });
}

#pragma mark - 文字提示信息HUD

/**
 纯文字提示HUD

 @param message 自定义文字提示
 */
- (QZHHUD *)textHUDWithMessage:(NSString *)message afterDelay:(NSTimeInterval)delay {
    QZHWS(weakSelf);
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf hiddenHUD];
        
        weakSelf.textHUD = [[MBProgressHUD alloc] initWithView:self.targetView];
        weakSelf.textHUD.mode = MBProgressHUDModeText;
        weakSelf.textHUD.label.text = message;
        weakSelf.textHUD.removeFromSuperViewOnHide = YES;
        
        [weakSelf showHUD:weakSelf.textHUD];
        [weakSelf.textHUD hideAnimated:YES afterDelay:delay];
    });
    
    return [QZHHUD HUD];
}

/**
 activity与文字提示HUD
 
 @param message 自定义文字提示
 */
- (QZHHUD *)textActivityWithMessage:(NSString *)message afterDelay:(NSTimeInterval)delay {
    QZHWS(weakSelf);
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf hiddenHUD];
        
        weakSelf.textHUD = [[MBProgressHUD alloc] initWithView:self.targetView];
        weakSelf.textHUD.mode = MBProgressHUDModeIndeterminate;
        weakSelf.textHUD.label.text = message;
        weakSelf.textHUD.removeFromSuperViewOnHide = YES;
        
        [weakSelf showHUD:weakSelf.textHUD];
        [weakSelf.textHUD hideAnimated:YES afterDelay:delay];
    });
    
    return [QZHHUD HUD];
}

/**
 文字图片提示HUD

 @param imageNamed 自定义图片
 @param message 提示文字
 */
- (QZHHUD *)textHUDWithImage:(NSString *)imageNamed message:(NSString *)message afterDelay:(NSTimeInterval)delay {
    QZHWS(weakSelf);
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf hiddenHUD];
        
        weakSelf.textHUD = [[MBProgressHUD alloc] initWithView:self.targetView];
        weakSelf.textHUD.mode = MBProgressHUDModeCustomView;
        weakSelf.textHUD.customView = [weakSelf gifViewWithLocal:imageNamed];
        weakSelf.textHUD.label.text = message;
        weakSelf.textHUD.removeFromSuperViewOnHide = YES;
        
        [weakSelf showHUD:self.textHUD];
        [weakSelf.textHUD hideAnimated:YES afterDelay:delay];
    });
    
    return [QZHHUD HUD];
}

/**
 显示HUD

 @param HUD 需要显示的HUD
 */
- (void)showHUD:(MBProgressHUD *)HUD {
    if (!self.targetView) {
        return;
    }
    [self.targetView addSubview:HUD];
    
    [HUD showAnimated:YES];
}

- (void)hiddenHUD {
    [self.loadHUD hideAnimated:YES];
    if (self.textHUD) {
        [self.textHUD hideAnimated:YES];
    }
}

/**
 加载自定义图片

 @param named 本地图片名称
 @return 自定义HUD view
 */
- (UIView *)gifViewWithLocal:(NSString *)named {
    UIImage *image = [UIImage imageNamed:named];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    imageView.image = image;
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    
    return imageView;
}

- (void)setQZHHUDFinish:(HudFinished)QZHHUDFinish {
    QZHWS(weakSelf);
    self.textHUD.completionBlock = ^{
        if (QZHHUDFinish) {
            QZHHUDFinish();
            weakSelf.textHUD.completionBlock = nil;
            weakSelf.QZHHUDFinish = nil;
        }
    };
}

/**
 获取当前vc
 
 @return 返回当前vc
 */
- (UIViewController *)getCurrentVC {
    UIViewController *vc = [[UIApplication sharedApplication] delegate].window.rootViewController;
    
    UIViewController *parent = vc;
    
    while ((parent = vc.presentedViewController) != nil ) {
        vc = parent;
    }
    
    while ([vc isKindOfClass:[UINavigationController class]]) {
        vc = [(UINavigationController *)vc topViewController];
    }
    
    if ([vc isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tab = (UITabBarController *)vc;
        if ([tab.selectedViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController *nav = (UINavigationController *)tab.selectedViewController;
            return [nav.viewControllers lastObject];
        } else {
            return tab.selectedViewController;
        }
    } else if ([vc isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nav = (UINavigationController *)vc;
        return [nav.viewControllers lastObject];
    } else {
        return vc;
    }
}


#pragma makr - lazy init

- (UIView *)targetView {
    return [self getCurrentVC].view;
}

- (MBProgressHUD *)loadHUD {
    
    if (!_loadHUD && self.targetView) {
        _loadHUD = [[MBProgressHUD alloc] initWithView:self.targetView];
    }

    return _loadHUD;
}

@end
